#include "math_lib.h"
#include <algorithm>
#include <cmath>
#include <numeric>

namespace MathLib {

double Statistics::mean(const std::vector<double>& data) {
    if (data.empty()) {
        return 0.0;
    }
    
    double sum = std::accumulate(data.begin(), data.end(), 0.0);
    return sum / data.size();
}

double Statistics::median(std::vector<double> data) {
    if (data.empty()) {
        return 0.0;
    }
    
    std::sort(data.begin(), data.end());
    size_t size = data.size();
    
    if (size % 2 == 0) {
        return (data[size/2 - 1] + data[size/2]) / 2.0;
    } else {
        return data[size/2];
    }
}

double Statistics::variance(const std::vector<double>& data) {
    if (data.size() <= 1) {
        return 0.0;
    }
    
    double avg = mean(data);
    double sum = 0.0;
    
    for (double value : data) {
        double diff = value - avg;
        sum += diff * diff;
    }
    
    return sum / (data.size() - 1);
}

double Statistics::standardDeviation(const std::vector<double>& data) {
    return std::sqrt(variance(data));
}

} // namespace MathLib